#!/usr/local/bin/perl -w

use App::Options (
    options => [qw(dbdriver dbclass dbhost dbname dbuser dbpass)],
    option => {
        dbclass  => { default => "App::Repository::MySQL", },
        dbdriver => { default => "mysql", },
        dbhost   => { default => "localhost", },
        dbname   => { default => "test2", },
        dbuser   => { default => "", },
        dbpass   => { default => "", },
    },
);

use Test::More qw(no_plan);
use lib "../App-Context/lib";
use lib "../../App-Context/lib";
use lib "lib";
use lib "../lib";
use lib ".";
use lib "t";

use App;
use App::Repository;
use RepositoryTestUtils qw(create_table_test_person drop_table_test_person populate_table_test_person);
use strict;

if (!$App::options{dbuser}) {
    ok(1, "No dbuser given. Tests assumed OK. (add dbuser=xxx and dbpass=yyy to app.conf in 't' directory)");
    exit(0);
}

my $context = App->context(
    conf_file => "",
    conf => {
        Repository => {
            default => {
                class => $App::options{dbclass},
                dbdriver => $App::options{dbdriver},
                dbhost => $App::options{dbhost},
                dbname => $App::options{dbname},
                dbuser => $App::options{dbuser},
                dbpass => $App::options{dbpass},
                table => {
                    test_person => {
                        primary_key => ["person_id"],
			                     alias => "tp",
			                     tablealiases => [ "tp" ],
			                     tablealias => {
			                         tp => {
                                table => "test_person",
			                         },
			                     },
                    },
                },
            },
        },
    },
    debug_sql => $App::options{debug_sql},
    trace => $App::options{trace},
);

my $rep = $context->repository();
&drop_table_test_person($rep);
&create_table_test_person($rep);
&populate_table_test_person($rep);

###########################################################################
# DATA ACCESS TESTS
###########################################################################
my ($person_id, $first_name, $last_name, $address, $city, $state, $zip, $country);
my ($home_phone, $work_phone, $email_address, $gender, $birth_dt, $age);

my $columns = [ "person_id", "age", "first_name", "gender", "state" ];
my $rows = [
    [ 1, 39, "stephen",   "M", "GA", ],
    [ 2, 37, "susan",     "F", "GA", ],
    [ 3,  6, "maryalice", "F", "GA", ],
    [ 4,  3, "paul",      "M", "GA", ],
    [ 5,  1, "christine", "F", "GA", ],
    [ 6, 45, "tim",       "M", "FL", ],
    [ 7, 39, "keith",     "M", "GA", ],
];

my ($row, $data_rows, $data_rows2, $nrows);

#####################################################################
#  $value  = $rep->get ($table, $key,     $col,   \%options);
#  $rep->set($table, $key,     $col,   $value,    \%options);
#####################################################################
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "stephen", "get() first_name [$first_name]");
is($rep->set("test_person", 1, "first_name", "steve"),1,"set() first name [steve]");
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "steve", "get() modified first_name [$first_name] (no cache)");

exit(0);

$first_name = $rep->get("test_person", {person_id => 1}, "first_name");
is($first_name, "steve", "get() modified first_name [$first_name]");
$age = $rep->get("test_person", 1, "age");
is($age, 39, "get() age");

ok($rep->set("test_person", 2, ["first_name","age"], ["sue",38]), "set() 2 values");
($first_name, $age) = $rep->get("test_person", 2, ["first_name","age"]);
is($first_name, "sue", "get() 2 values (checking 1 of 2)");
is($age, 38, "get() 2 values (checking 2 of 2)");

ok($rep->set_row("test_person", 3, ["age", "state"], [7, "CA"]),"set_row() 2 values");
$row = $rep->get_row("test_person", 4, ["age", "gender"]);
($age, $gender) = @$row;
is($age, 3, "get_row() 2 values (checking 1 of 2)");
is($gender, "M", "get_row() 2 values (checking 2 of 2)");

ok($rep->set_row("test_person", {first_name=>'paul'}, ["age", "state"], [5, "CA"]),"set_row() 2 values w/ %crit");
$row = $rep->get_row("test_person", {first_name=>'paul'}, ["age", "state","person_id"]);
($age, $state, $person_id) = @$row;
is($age,         5, "get_row() 3 values w/ %crit (checking 1 of 3) age=$age");
is($state,    "CA", "get_row() 3 values w/ %crit (checking 2 of 3) state=$state");
is($person_id,   4, "get_row() 3 values w/ %crit (checking 3 of 3) person_id=$person_id");

ok($rep->set_row("test_person", {first_name=>'paul'}, ["age", "state"], {age=>6, state=>"GA", person_id=>99}),
   "set_row() 2 values w/ %crit and values in hash");

$row = $rep->get_row("test_person", {first_name=>'paul'}, ["age", "state","person_id"]);
($age, $state, $person_id) = @$row;
is($age,         5, "get_row() 3 values w/ %crit (checking 1 of 3) age=$age got cache instead");
is($state,    "CA", "get_row() 3 values w/ %crit (checking 2 of 3) state=$state got cache instead");
is($person_id,   4, "get_row() 3 values w/ %crit (checking 3 of 3) person_id=$person_id");

$data_rows = $rep->get_rows("test_person", {first_name=>'paul', x=>1}, ["age", "state","person_id"]);
$row = $data_rows->[0];
($age, $state, $person_id) = @$row;
is($age,         6, "get_row() 3 values w/ %crit (checking 1 of 3) age=$age");
is($state,    "GA", "get_row() 3 values w/ %crit (checking 2 of 3) state=$state");
is($person_id,   4, "get_row() 3 values w/ %crit (checking 3 of 3) person_id=$person_id");

$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", "first_name", "gender", "state" ], {order_by=>["person_id"]});
is_deeply($data_rows, $rows, "get_rows() got original cached data thanks to cache_minimum_rows");

$data_rows2 = $rep->{dbh}->selectall_arrayref("select person_id, age, first_name, gender, state from test_person order by person_id");
$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", "first_name", "gender", "state" ], {order_by=>["person_id"], cache_skip => 1});
is_deeply($data_rows, $data_rows2, "get_rows() skipped cached data thanks to cache_skip");
$first_name = $rep->get("test_person", 1, "first_name", { cache_skip => 1 });
is($first_name, "steve", "get() modified first_name [$first_name] by skipping the cache");

$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", "first_name", "gender", "state" ], {order_by=>["person_id"], cache_refresh => 1});
is_deeply($data_rows, $data_rows2, "get_rows() refreshed cached data thanks to cache_refresh");
$first_name = $rep->get("test_person", 1, "first_name", { cache_refresh => 1 });
is($first_name, "steve", "get() modified first_name [$first_name] by refreshing the cache");

$data_rows = $rep->get_rows("test_person", {}, [ "person_id", "age", "first_name", "gender", "state" ], {order_by=>["person_id"]});
is_deeply($data_rows, $data_rows2, "get_rows() confirmed that the cache was refreshed");
$first_name = $rep->get("test_person", 1, "first_name");
is($first_name, "steve", "get() modified first_name [$first_name] confirming that the cache was refreshed");

{
    &drop_table_test_person($rep);
}

exit 0;

